package com.helen.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.helen.entity.OrderListEntity;
import com.helen.shopping.R;
import com.helen.ui.order.CommentProductActivity;
import com.helen.ui.order.CommentProductDetailActivity;
import com.helen.ui.order.HandleOrderStatus;
import com.helen.ui.order.PaidOrderActivity;
import com.helen.utils.ImageLoadUtil;
import com.helen.utils.MyLog;
import com.helen.utils.tiputils.MToast;

import java.io.Serializable;
import java.util.List;


/**
 * Created by yang on 2018/5/2.
 */

public class OrderListAdapter extends BaseQuickAdapter<OrderListEntity, BaseViewHolder> {
    //    0、全部 1、待付款 2、待发货 3、待收货 4、待评价 5、交易完成  6、已删除 7、已取消
    private Intent ti = null;
    private HandleOrderStatus handleOrderStatus;

    public OrderListAdapter(Context context, int layoutResId, List<OrderListEntity> data) {
        super(layoutResId, data);
        handleOrderStatus = new HandleOrderStatus(context);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final OrderListEntity item) {
        try {
            TextView tvStatus = (TextView) helper.getView(R.id.tv_status);
            final ImageView imgProduct = (ImageView) helper.getView(R.id.img_product);
            TextView tvProductName = (TextView) helper.getView(R.id.tv_product_name);
            TextView tvProductSpec = (TextView) helper.getView(R.id.tv_product_spec);
            TextView tvProductPrice = (TextView) helper.getView(R.id.tv_product_price);
            TextView tvProductCount = (TextView) helper.getView(R.id.tv_product_count);
            TextView tvTotalPrice = (TextView) helper.getView(R.id.tv_total_price);
            LinearLayout llOrderBottom = (LinearLayout) helper.getView(R.id.ll_order_bottom);
            TextView tvBottomOrderLeft = (TextView) helper.getView(R.id.tv_bottom_order_left);
            TextView tvBottomOrderRight = (TextView) helper.getView(R.id.tv_bottom_order_right);
            final int status = item.getState();
            switch (status) {
                case 0:
                    break;
                case 1:
                    tvStatus.setText("待付款");
                    tvBottomOrderLeft.setVisibility(View.VISIBLE);
                    tvBottomOrderRight.setVisibility(View.VISIBLE);
                    tvBottomOrderLeft.setText("取消订单");
                    tvBottomOrderRight.setText("付款");
                    break;
                case 2:
                    tvStatus.setText("待发货");
                    tvBottomOrderRight.setVisibility(View.GONE);
//                    tvBottomOrderLeft.setText("查看物流");
                    tvBottomOrderLeft.setVisibility(View.GONE);
                    llOrderBottom.setVisibility(View.GONE);
                    break;
                case 3:
                    tvStatus.setText("待收货");
                    tvBottomOrderRight.setText("确认收货");
                    tvBottomOrderRight.setVisibility(View.VISIBLE);
//                    tvBottomOrderLeft.setText("查看物流");
                    tvBottomOrderLeft.setVisibility(View.GONE);
                    break;
                case 4:
                    tvStatus.setText("待评价");
                    tvBottomOrderRight.setText("去评价");
                    tvBottomOrderRight.setVisibility(View.VISIBLE);
//                    tvBottomOrderLeft.setText("查看物流");
                    tvBottomOrderLeft.setVisibility(View.GONE);
                    break;
                case 5:
                    tvStatus.setText("交易完成");
                    tvBottomOrderLeft.setVisibility(View.VISIBLE);
                    tvBottomOrderLeft.setText("删除订单");
                    tvBottomOrderRight.setText("查看评价");
                    tvBottomOrderRight.setVisibility(View.VISIBLE);
                    break;
                case 6:
                    tvStatus.setText("已删除");
                    tvBottomOrderLeft.setVisibility(View.GONE);
                    tvBottomOrderRight.setVisibility(View.GONE);
                    break;
                case 7:
                    tvStatus.setText("已取消");
                    llOrderBottom.setVisibility(View.GONE);
                    break;

            }
            ImageLoadUtil.displayImage(mContext, imgProduct, item.getProductimg(), R.mipmap.app_logo, R.mipmap.app_logo);
            tvProductName.setText(item.getProductname());
            if (!TextUtils.isEmpty(item.getSpec())) {
                tvProductSpec.setText(item.getSpec());
                tvProductSpec.setVisibility(View.VISIBLE);
            } else {
                tvProductSpec.setVisibility(View.GONE);
            }
            tvProductPrice.setText("￥" + item.getOprice());
            tvProductCount.setText("x" + item.getCount());
            tvTotalPrice.setText("￥" + String.format("%.2f",item.getPrice()) + "(含运费" + item.getExpressprice() + ")");


            tvBottomOrderLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (status) {
                        case 0:
                            break;
                        case 1:
                            MToast.makeTextLong(mContext, "取消订单").show();
                            handleOrderStatus.closeOrderApi(mContext, 7, item.getId());
                            break;
                        case 2:
                            MToast.makeTextLong(mContext, "查看物流").show();
                            break;
                        case 3:
                            MToast.makeTextLong(mContext, "查看物流").show();
                            break;
                        case 4:
                            MToast.makeTextLong(mContext, "查看物流").show();
                            break;
                        case 5:
                            MToast.makeTextLong(mContext, "删除订单").show();
                            handleOrderStatus.closeOrderApi(mContext, 6, item.getId());
                            break;
                        case 6:
                            break;
                        case 7:
                            break;

                    }
                }
            });


            tvBottomOrderRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (status) {
                        case 0:
                            break;
                        case 1:
                            ti = new Intent(mContext, PaidOrderActivity.class);
                            ti.putExtra("s_orderid", item.getId());
                            ti.putExtra("allPrice", item.getPrice() + "");
                            ti.putExtra("fromType", 1);
                            mContext.startActivity(ti);
                            break;
                        case 2:
                            break;
                        case 3:
                            handleOrderStatus.closeOrderApi(mContext, 5, item.getId());
                            break;
                        case 4:
                            Intent ti = new Intent(mContext, CommentProductActivity.class);
                            ti.putExtra("item", (Serializable) item);
                            mContext.startActivity(ti);
                            break;
                        case 5:
                            Intent ti2 = new Intent(mContext, CommentProductDetailActivity.class);
                            ti2.putExtra("iid", item.getCommentid());
                            mContext.startActivity(ti2);
                            break;
                        case 6:
                            break;
                        case 7:
                            break;

                    }
                }
            });

        } catch (Exception e) {
            MyLog.e("yang", "设置订单列表异常" + e.toString());
        }


    }


}
